package com.qf.fmall.index.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.qf.fmall.index.entity.Category;
import com.qf.fmall.index.entity.IndexImg;
import com.qf.fmall.index.entity.Product;
import com.qf.fmall.index.service.ICategoryService;
import com.qf.fmall.index.service.IIndexImgService;
import com.qf.fmall.index.service.IProductService;
import com.qf.fmall.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 轮播图 前端控制器
 * </p>
 *
 * @author djl
 * @since 2024-04-25
 */
@CrossOrigin
@RestController
@RequestMapping("/index")
public class IndexController {

    @Autowired
    private IIndexImgService indexImgService;

    @Autowired
    private ICategoryService categoryService;


    @Autowired
    private IProductService productService;


    @GetMapping("/category-recommends")
    public ResultVo categoryRecommends() throws InterruptedException {

        //1. 打印日志
        //2. 校验参数
        //3. 调用service完成业务逻辑
        List<Category> data =  categoryService.categoryRecommends();
        //4. 返回json
        return new ResultVo(10000, "success", data);
    }


    @GetMapping("/list-recommends")
    public ResultVo listRecommends(){

        //1. 打印日志
        //2. 校验参数
        //3. 调用service完成业务逻辑

        List<Product> data = productService.listRecommends();

        //4. 返回json
        return new ResultVo(10000, "success", data);

    }


    @GetMapping("/category-list")
    public ResultVo categoryList(){
        //1. 打印日志
        //2. 校验参数
        //3. 调用service完成业务逻辑
//        List<Category> data =  categoryService.categoryList();

        List<Category> data = categoryService.categoryListWithOneSql();
        //4. 返回json
        return new ResultVo(10000, "success", data);
    }

    @GetMapping("/indeximg")
    public ResultVo indeximg(){

        //1. 打印日志
        //2. 校验参数

        //3. 调用service完成业务逻辑
        QueryWrapper<IndexImg> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",1) // 有效的轮播图
                .orderByAsc("seq");// order by seq asc
        List<IndexImg> data = indexImgService.list(queryWrapper);

        //4. 返回json
        return new ResultVo(10000, "success", data);
    }

}
